package org.mybatis.example;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import javax.sql.DataSource;
import java.io.InputStream;

/**
 * 分析入口
 * <p>
 * 官网提供两种方式：xml/类
 *
 *      这两种方式最终都会调用
 *          new DefaultSqlSessionFactory(config)
 *      返回SqlSessionFactory
 *
 * @author Caleb
 * @date 2021/3/19 9:26
 * @description
 */
public class TestMain {


  /**
   * 通过配置文件方式获取
   * 1.加载配置文件：
   *      Resources.getResourceAsStream("org/mybatis/example/mybatis.xml")
   *
   * 2.解析配置文件：
   *      XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
   * 	    该方法内部会创建XPathParser解析器
   * 	    通过jdk的javax.xml.parsers包处理xml文件，创建Document对象
   *
   * 3.创建Configuration对象：
   * 	    parser.parse()利用xpath获取节点并转换成Configuration并对其属性赋值
   *
   * 4.创建SqlSessionFactory对象：
   *      new DefaultSqlSessionFactory(config);
   *
   * @return
   * @throws Exception
   */
  public static SqlSessionFactory adoptXml() throws Exception {
    // 获取文件输入流
    String resource = "org/mybatis/example/mybatis-config.xml";

    // 返回一个在classpath下的资源流对象
    InputStream inputStream = Resources.getResourceAsStream(resource);

    // 通过InputStream创建一个SqlSessionFactory对象
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().
        build(inputStream);

    return sqlSessionFactory;
  }


  /**
   * 通过类方式获取
   * 1.对Configuration进行属性赋值即可
   *
   * 2.创建SqlSessionFactory对象：
   *    new DefaultSqlSessionFactory(config);
   * @return
   */
  public static SqlSessionFactory adoptClass() {
    DataSource dataSource = dataSource();
    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    Environment environment = new Environment("development", transactionFactory, dataSource);
    Configuration configuration = new Configuration(environment);
    configuration.addMapper(BlogMapper.class);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
    return sqlSessionFactory;
  }

  public static DruidDataSource dataSource(){
    DruidDataSource druidDataSource = new DruidDataSource();
    druidDataSource.setUrl("jdbc:mysql://localhost:3306/test?autoReconnect=false&useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai");
    druidDataSource.setUsername("root");
    druidDataSource.setPassword("password");
    return druidDataSource;
  }

  public static void main(String[] args) throws Exception {
    SqlSessionFactory sqlSessionFactory = adoptClass();
    // 获取Mapper
    BlogMapper mapper = sqlSessionFactory.openSession().getMapper(BlogMapper.class);
    Blog blog = mapper.selectBlog(1);
//    System.out.println(blog);
  }
}
